from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from utils.actuator import actuator 


DEFAULT_SETTING = {
  'host': '127.0.0.1',
  'port': 3306,
  'database': 'su_ning',
  'username': 'root',
  'password': '123456',
}


class DB:

  def __init__(self, conf = DEFAULT_SETTING) -> None:
    self.conf = conf
    self.engine = None
    self.DBSession = None

  def connect(self):
    db_args =  'mysql+pymysql://{username}:{password}@{host}:{port}/{database}?charset=utf8'.format(
      username = self.conf['username'],
      password = self.conf['password'],
      host = self.conf['host'],
      port = self.conf['port'],
      database = self.conf['database']
    )

    d = create_engine(db_args, encoding='utf-8')

    self.engine = d
    self.DBSession = sessionmaker(bind=d)
    print('数据库连接成功 !!!')
    return True
  

  